<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{ cateName }}</XtxBreadItem>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxSlider :sliders="bannerList" style="height: 500px" />
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="it in cateList" :key="it.id">
            <a href="javascript:;">
              <img :src="it.picture" />
              <p>{{ it.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in cateList" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <XtxMore to="/" />
        </div>
        <div class="body">
          <GoodsItem v-for="good in item.goods" :key="good.id" :goods="good"></GoodsItem>
          <!-- <RouterLink to="/" class="goods-item" v-for="good in item.goods" :key="good">
            <img v-imgLazy="good.picture" alt="" />
            <p class="name ellipsis">{{ good.name }}</p>
            <p class="desc ellipsis">{{ good.desc }}</p>
            <p class="price">&yen;{{ good.price }}</p>
          </RouterLink> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
// import { watch } from 'vue'
import { findTopCategory } from '@/api/category.js'
import { useBanner } from '../Home/server.js'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import GoodsItem from './conponents/GoodsItenm.vue'
export default {
  name: 'CateGory',
  components: { GoodsItem },
  setup() {
    const cateName = ref('')
    const cateList = ref([])
    const route = useRoute()
    console.log(route.params.id)
    const bannerList = useBanner()
    findTopCategory(route.params.id).then(res => {
      console.log(res)
      cateName.value = res.result.name
      cateList.value = res.result.children
    })
    // watch(
    //   () => route.params.id,
    //   (newVal, oldVal) => {
    //     console.log('id变了', newVal)
    //     // 获取一级类目的数据
    //     findTopCategory(route.params.id).then(res => {
    //       console.log(res)
    //       cateName.value = res.result.name
    //       cateList.value = res.result.children
    //     })
    //   },
    //   { immediate: true }
    // )
    onBeforeRouteUpdate(to => {
      // 获取一级类目的数据
      findTopCategory(to.params.id).then(res => {
        console.log(res)
        cateName.value = res.result.name
        cateList.value = res.result.children
      })
    })
    return { bannerList, cateName, cateList }
  }
}
</script>

<style lang="less" scoped>
.goods-item {
  display: block;
  width: 220px;
  padding: 20px 30px;
  text-align: center;
  img {
    width: 160px;
    height: 160px;
  }
  p {
    padding-top: 10px;
  }
  .name {
    font-size: 16px;
  }
  .desc {
    color: #999;
    height: 29px;
  }
  .price {
    color: @priceColor;
    font-size: 20px;
  }
}
</style>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      padding: 0 65px 30px;
      flex-wrap: wrap;
    }
  }
}
</style>
